package com.wdy.service.impl;

import java.util.LinkedList;
import java.util.List;

import com.wdy.dao.RepairDao;
import com.wdy.po.Repair;
import com.wdy.po.Tenement;
import com.wdy.service.RepairManager;
import com.wdy.util.PageUtil;
import com.wdy.vo.Condition;
import com.wdy.vo.Page;
import com.wdy.vo.Result;

@SuppressWarnings("unchecked")
public class RepairManagerImpl implements RepairManager {

	private RepairDao repairDao;

	public void setRepairDao(RepairDao repairDao) {
		this.repairDao = repairDao;
	}

	public void saveRepair(Repair Repair) {
		repairDao.save(Repair);
	}

	public void removeRepair(Repair Repair) {
		repairDao.delete(Repair);
	}

	public void updateRepair(Repair repair) {
		repairDao.attachDirty(repair);
	}

	public Repair getRepair(Integer id) {
		return repairDao.findById(id);
	}

	public List<Repair> listAll() {
		return repairDao.findAll();
	}

	public List<Repair> findByExample(Repair repair) {
		return repairDao.findByExample(repair);
	}

	public List<Repair> findByDate(String value) {
		return repairDao.findByDate(value);
	}

	public List<Tenement> findTenement(Object value) {
		return repairDao.findTenement(value);
	}

	@Override
	public Result query(List<Condition> conList, Page page) {
		// TODO Auto-generated method stub
		int totalRecords = repairDao.getQueryCount(conList);
		page = PageUtil.createPage(page, totalRecords);
		List tempList = (List) repairDao.query(conList, page);
		List<Repair> repairs = new LinkedList<Repair>();
		for (Object o : tempList) {
			if (o instanceof Object[]) {
				Object[] oList = (Object[]) o;
				for (Object oo : oList) {
					if (oo instanceof Repair) {
						repairs.add((Repair) oo);
					}
				}
			} else {
				if (o instanceof Repair) {
					repairs.add((Repair) o);
				}
			}
		}
		return new Result(page, repairs);
	}

	@Override
	public void removeRepairs(Object[] objects) {
		// TODO Auto-generated method stub
		repairDao.deleteBatch(objects);
	}
}
